package com.cuit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.AddressMapper;
import com.cuit.mapper.UserEditMapper;
import com.cuit.pojo.Address;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.AddressService;
import com.cuit.service.UserEditService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags="地址信息设置")
@CrossOrigin
@RequestMapping("/address_edit")
public class AddressEditController {


    @Autowired
    private UserEditService userEditService;

    @Autowired
    private AddressService addressService;


    @Autowired
    private UserEditMapper userEditMapper;

    @Autowired
    private AddressMapper addressMapper;

    //通过用户id和类型查询地址列表
    @RequestMapping("/addressByUserIdAndType/user_id={user_id}&type={type}")
    @ApiOperation("通过用户id和类型查询地址列表")
    public List<Address> addressByUserIdAndType(@PathVariable String user_id,@PathVariable String type){
        QueryWrapper<Address> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(Address::getUser_id,user_id)
                .eq(Address::getType,type);
        List<Address> list = addressService.list(queryWrapper);
        return list;
    }

    //通过id查询具体地址
    @RequestMapping("/addressById/{id}")
    @ApiOperation("通过id查询具体地址")
    public Address addressById(@PathVariable String id){
        System.out.println(id);
        Address address = addressMapper.selectById(id);
        System.out.println(address);
        return address;
    }

    //修改地址
    @RequestMapping("addressEdit")
    @ApiOperation("修改地址")
    public void addressEdit(@RequestBody Address address){
        System.out.println("isDefault:"+address.getIsDefault());
        QueryWrapper<Address> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(Address::getUser_id,address.getUser_id())
                .eq(Address::getType,address.getType());
        List<Address> list = addressService.list(queryWrapper);
        if(address.getIsDefault()==1){
            for(int i=0;i<list.size();i++){
                if(list.get(i).getIsDefault()==1){
                    list.get(i).setIsDefault(0);
                    addressMapper.updateById(list.get(i));
                    break;
                }
            }
        }
        System.out.println("更改以后:"+address.getIsDefault());
        int result = addressMapper.updateById(address);
        System.out.println(result);
    }

    //添加地址
    @RequestMapping("/addressAdd")
    @ApiOperation("添加地址")
    public R addressAdd(@RequestBody Address address){
        QueryWrapper<Address> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(Address::getUser_id,address.getUser_id())
                .eq(Address::getType,address.getType());
        List<Address> list = addressService.list(queryWrapper);
        if(address.getIsDefault()==1){
            for(int i=0;i<list.size();i++){
                if(list.get(i).getIsDefault()==1){
                    list.get(i).setIsDefault(0);
                    addressMapper.updateById(list.get(i));
                    break;
                }
            }
        }
        addressMapper.insert(address);
        return R.ok();
    }

    //根据Id删除
    @RequestMapping("/deleteById/{id}&{type}&{user_id}")
    @ApiOperation("根据Id删除")
    public R deleteById(@PathVariable String id,@PathVariable String type,@PathVariable String user_id){
        int i = addressMapper.deleteById(id);
        QueryWrapper<Address> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Address::getUser_id,user_id)
                .eq(Address::getType,type);
        List<Address> address_list = addressMapper.selectList(queryWrapper);
        List list= Lists.newArrayList();
        list.add(address_list);
        list.add(i);
        return R.ok().data("result_list",list);
    }

    //根据用户名查询默认发货地址
    @RequestMapping("/PostAddressByUsername/{username}")
    @ApiOperation("根据用户名查询默认发货地址")
    public String AddressByUsername(@PathVariable String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,username);
        User user = userEditService.getOne(queryWrapper);
        QueryWrapper<Address> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(Address::getType,2).eq(Address::getUser_id,user.getId());
        List<Address> list = addressMapper.selectList(queryWrapper1);
        return list.get(0).getAddressDetail();
    }
}
